package public

import (
	"fmt"
	"math"
	"strconv"
	"time"
)

//WeekDate 时间类
type WeekDate struct {
	WeekTh    string
	StartTime time.Time
	EndTime   time.Time
}

//WeekByToday 判断今天是第几周
func WeekByToday() string {
	dateTime := time.Now()
	return WeekByDate(dateTime)
}

//WeekByDate 判断时间是当年的第几周
func WeekByDate(t time.Time) string {
	yearDay := t.YearDay()
	yearFirstDay := t.AddDate(0, 0, -yearDay+1)
	firstDayInWeek := int(yearFirstDay.Weekday())

	//今年第一周有几天
	firstWeekDays := 1
	if firstDayInWeek != 0 {
		firstWeekDays = 7 - firstDayInWeek + 1
	}
	var week int
	if yearDay <= firstWeekDays {
		week = 1
	} else {
		week = (yearDay-firstWeekDays)/7 + 2
	}
	return fmt.Sprintf("%d第%d周", t.Year(), week)
}

//GroupByWeekDate 将开始时间和结束时间分割为周为单位
func GroupByWeekDate(startTime, endTime time.Time) []WeekDate {
	weekDate := make([]WeekDate, 0)
	diffDuration := endTime.Sub(startTime)
	days := int(math.Ceil(float64(diffDuration/(time.Hour*24)))) + 1

	currentWeekDate := WeekDate{}
	currentWeekDate.WeekTh = WeekByDate(endTime)
	currentWeekDate.EndTime = endTime
	currentWeekDay := int(endTime.Weekday())
	if currentWeekDay == 0 {
		currentWeekDay = 7
	}
	currentWeekDate.StartTime = endTime.AddDate(0, 0, -currentWeekDay+1)
	nextWeekEndTime := currentWeekDate.StartTime
	weekDate = append(weekDate, currentWeekDate)

	for i := 0; i < (days-currentWeekDay)/7; i++ {
		weekData := WeekDate{}
		weekData.EndTime = nextWeekEndTime
		weekData.StartTime = nextWeekEndTime.AddDate(0, 0, -7)
		weekData.WeekTh = WeekByDate(weekData.StartTime)
		nextWeekEndTime = weekData.StartTime
		weekDate = append(weekDate, weekData)
	}

	if lastDays := (days - currentWeekDay) % 7; lastDays > 0 {
		lastData := WeekDate{}
		lastData.EndTime = nextWeekEndTime
		lastData.StartTime = nextWeekEndTime.AddDate(0, 0, -lastDays)
		lastData.WeekTh = WeekByDate(lastData.StartTime)
		weekDate = append(weekDate, lastData)
	}

	return weekDate
}

// TimeSubDays op
func TimeSubDays(t1, t2 time.Time) int {

	if t1.Location().String() != t2.Location().String() {
		return -1
	}
	hours := t1.Sub(t2).Hours()

	if hours <= 0 {
		return -1
	}
	// sub hours less than 24
	if hours < 24 {
		// may same day
		t1y, t1m, t1d := t1.Date()
		t2y, t2m, t2d := t2.Date()
		isSameDay := (t1y == t2y && t1m == t2m && t1d == t2d)

		if isSameDay {

			return 0
		}
		return 1

	} else if (hours/24)-float64(int(hours/24)) == 0 {

		return int(hours / 24)

	}
	return int(hours/24) + 1

}

// getDayOfyear is getDayOfyear
func getDayOfyear() string {
	var timestamp int64 = 1577808005 //2020-01-01
	begin := time.Unix(timestamp, 0)
	end := time.Now()
	cost := strconv.Itoa(TimeSubDays(end, begin))

	content := `坚持 
即使失去希望，也要早睡早起，
--任务1 拒绝腾讯视频小程序
看任何综艺，电视剧和电影 动漫
--任务2 拒绝手机上浏览器看小说
基本事情要做到 小目标。
今天是第`

	content += cost + "天, 每周总结：https://github.com/wangcy6/weekly,每日复盘:https://wangcy6.github.io"
	return content
}

// GetBeijingTime is local time
func GetBeijingTime() string {
	// 获取当前(当地)时间
	t := time.Now()
	// 获取0时区时间
	t = time.Now().UTC()
	//fmt.Println(t)
	// 获取当前时间戳
	timestamp := t.Unix()
	//fmt.Println(timestamp)
	// 获取时区信息
	name, offset := t.Zone()
	fmt.Println(name, offset)
	// 把时间戳转换为时间
	currenttime := time.Unix(timestamp+int64(offset), 0)
	// 格式化时间
	//fmt.Println("Current time : ", currenttime.Format("2006-01-02 15:04:05"))
	return currenttime.Format("2006-01-02 15:04:05")
}

//GetLocatTime 获取当地时间
func GetLocatTime() string {
	// 获取当前(当地)时间
	t := time.Now().Local()

	// 格式化时间
	//fmt.Println("Current time : ", t.Format("2006-01-02 15:04:05"))
	return t.Format("2006-01-02 15:04:05")
}

// GetDayFromBegin 统计截止到今天已经过多少天
func GetDayFromBegin(timestamp int64) string {
	begin := time.Unix(timestamp, 0)
	end := time.Now()
	cost := strconv.Itoa(TimeSubDays(end, begin))
	return cost
}

//GetDayFrom20200101 统计截止到2020-01-01已经过多少天
func GetDayFrom20200101() string {

	return GetDayFromBegin(1577808005)
}

//DuringDays 统计截止到2020-01-01已经过多少天
func DuringDays() string {
	content := "今天是 "
	content += WeekByToday()
	content += "\r\n"

	content += " 365天中的第"
	days := GetDayFromBegin(1577808005)
	content += days
	content += "天,不知不觉一年已经过去"

	i, err := strconv.Atoi(days)
	if err != nil {
		i = 0
	}

	s := fmt.Sprintf("%d%% ", i*100/365)
	content += s
	content += "\r\n"
	content += "                                     "
	content += GetLocatTime()

	return content
}

// GetDayFromStringTime  2020-04-04 08:43:42
func GetDayFromStringTime(datetime string) string {

	//datetime 待转化为时间戳的字符串
	//日期转化为时间戳
	timeLayout := "2006-01-02 15:04:05"  //转化所需模板
	loc, _ := time.LoadLocation("Local") //获取时区
	tmp, _ := time.ParseInLocation(timeLayout, datetime, loc)
	timestamp := tmp.Unix() //转化为时间戳 类型是int64
	//fmt.Println(timestamp)

	return GetDayFromBegin(timestamp)

}
